#include <iostream>
using namespace std;

const int N = 100010;
int q[N], Size[N];
int n, m, a, b;
string op;

void init(int n)
{
    for (int i = 0; i <= n; ++i)
    {
        q[i] = i;
        Size[i] = 1;
    }
    return;
}

int find(int x)
{
    if (x != q[x])
        q[x] = find(q[x]);
    return q[x];
}

void merge(int a, int b)
{
    if (find(a) != find(b))
    {
        Size[find(b)] += Size[find(a)];
        q[find(a)] = find(b);
    }
    return;
}

bool query(int a, int b)
{
    return find(a) == find(b);
}

int main()
{
    scanf("%d%d", &n, &m);
    init(n);
    while (m--)
    {
        cin >> op;
        if (op == "C")
        {
            cin >> a >> b;
            merge(a, b);
        }
        else if (op == "Q1")
        {
            cin >> a >> b;
            if (query(a, b))
                printf("Yes\n");
            else
                printf("No\n");
        }
        else
        {
            cin >> a;
            printf("%d\n", Size[find(a)]);
        }
    }
    return 0;
}